<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="#">新闻管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>新闻列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--  输入框组-->
    <div>
      <br/>
      <span>新闻名称 &nbsp;&nbsp;</span>
      <el-input
          placeholder="请输入新闻名称"
          prefix-icon="el-icon-search"
          v-model="name" class="inputMax" >
      </el-input>
      <span>&nbsp;&nbsp;新闻类型&nbsp;&nbsp;</span>
      <el-select v-model="articleType" placeholder="请选择" class="inputMax">
        <el-option
            v-for="item in articleTypes"
            :key="item.id"
            :label="item.name"
            :value="item.name">
        </el-option>
      </el-select>
      &nbsp;&nbsp;
      <el-button type="primary" icon="el-icon-search" @click="articleSearch" ><span>查询</span></el-button>
      <br/>
      <br/>
    </div>
    <el-table
        :data="articles"
        border
        style="width: 100%">
      <el-table-column
          prop="id"
          label="ID"
          width="50">
      </el-table-column>
      <el-table-column
          prop="name"
          label="新闻名称"
          width="200">
      </el-table-column>
      <el-table-column
          prop="type"
          label="新闻类型">
      </el-table-column>
      <el-table-column
          prop="createTime"
          label="发布时间"
          width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime | DateFormat }}</span>
        </template>
      </el-table-column>
      <el-table-column
          prop="content"
          label="新闻内容"
          show-overflow-tooltip
          width="400">
      </el-table-column>
      <el-table-column
          fixed="right"
          width="100"
          label="操作">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="toArticle(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        background
        layout="prev, pager, next"
        :page-size="pageSize"
        :total="total">
    </el-pagination>

    <el-dialog title="新闻类型" :visible.sync="dialogFormVisible" :modal-append-to-body="false">
      <el-form :model="articleForm">
        <el-form-item label="新闻名称" :label-width="formLabelWidth">
          <el-input v-model="articleForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新闻内容" :label-width="formLabelWidth">
          <el-input type="textarea" :rows="2" v-model="articleForm.content" autocomplete="off"></el-input>
        </el-form-item>
        <img :src="preUrl+articleForm.image" style="width: 100%;">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="articleNoPass(articleForm)">打回</el-button>
        <el-button type="primary" @click="articlePass(articleForm)">通过</el-button>
      </div>

    </el-dialog>


  </div>

</template>

<script>
import {articleList,  articleTypeList, articleUpdate,articleById} from "@/api";

export default {
  name: "ArticleList",
  components: {
  },
  data() {
    return {
      name:'',
      articleTypes:[],
      articleType: '',
      articles:[],
      total: 0,
      pageSize: 0,
      articleForm:{},
      formLabelWidth: '120px',
      preUrl:'https://yashuang.oss-cn-beijing.aliyuncs.com/',
      dialogTableVisible: false,
      dialogFormVisible: false,
    }
  },

  created() {
    articleTypeList().then((result)=>{
      this.articleTypes = result.data;
      this.articleTypes.unshift({id:0,name:''})
    });
    let article = {status:1};
    articleList(article).then((result)=>{
      this.articles = result.data.list;
      this.total = result.data.total;
      this.pageSize = result.data.pagesize;
    })
  },

  methods: {
    articleSearch(){
      let article = {
        name: this.name,
        type: this.articleType,
        status:1,
      };
      articleList(article).then((result)=>{
        this.articles = result.data.list;
        this.total = result.data.total;
        this.pageSize = result.data.pagesize;
      });
    },

    toArticle(article){
      this.dialogFormVisible = true;
      this.articleForm = article;
    },

    articlePass(article){
      this.dialogFormVisible =false;
      article.status=2;
      articleUpdate(article).then(result=>{
        articleList({status:1}).then((result)=>{
          this.articles = result.data.list;
          this.total = result.data.total;
          this.pageSize = result.data.pagesize;
        })
      });

    },
    articleNoPass(article){
      this.dialogFormVisible = false;
      article.status = 0;
      articleUpdate(article).then(result=>{
        articleList({status:1}).then((result)=>{
          this.articles = result.data.list;
          this.total = result.data.total;
          this.pageSize = result.data.pagesize;
        })
      });
    }
  }
}
</script>

<style scoped>
.inputMax{
  width: 200px;
}
</style>
